<template>
  <div class="BpmInstStart">
    <RxLoading v-if="Loading"></RxLoading>
    <rx-layout v-else>
      <template v-slot:center>
        <rx-form  v-if="formJs.data" ref="rxForms"
                  :context-data="contextData"
                  :action="action" :formJs="formJs" :resForm="resForm"></rx-form>
      </template>
      <template v-slot:footer>
        <div class="tabs-toolbar">
          <process-tool-bar
              v-if="!preview"
              :processConfig="processConfig"
              :def-id="localDefId"
              :instId="instId"
              :mainDefId="mainDefId"
              :mainTaskId="mainTaskId"
              :appId="appId"
              :vars="vars"/>
        </div>
      </template>
    </rx-layout>
  </div>
</template>

<script>
import Util from "@/js/Util.js";
import BpmInstApi from "../../../api/rxBpm/bpmInst";
import RxForm from "../../../views/modules/form/customForm/RxForm"
import RxLayout from '@/components/RxLayout'
import ProcessToolBar from "./ProcessToolBar"
import RxLoading from '@/components/RxLoading'
export default {
  name: "BpmInstStart",
  components:{
    RxForm,
    RxLayout,
    ProcessToolBar,
    RxLoading
  },
  data(){
    var hasPk = false;
    if (this.pkId) {
      hasPk = true;
    }
    return {
      processConfig: {
        buttons: [],
        startConfirm: false,
        fillOpinion: false,
        assignFlowUsers: false,
        assignBatchFlowUsers: false,
        startCalFlowusers: false,
        //是否允许选择路径。
        allowSelectPath: false,
        formType: "online",
        hasPk: hasPk
      },
      //流程定义KEY
      defKey: "",
      //流程定义ID
      localDefId: "",
      //是否从路由过来
      fromRoute: false,
      //根实例
      rootVm: true,
      formDataJson: {},
      isShowImage: false,
      ////
      formJs:{},
      action:'edit',//状态edit编辑，detail明细
      resForm:{},
      contextData:{},
      Loading:true,
    }
  },
  props: {
    //流程定义ID
    defId: String,
    //流程实例ID
    instId: String,
    //通过主键直接启动流程
    pkId: String,
    config: Object,
    preview: Boolean,
    startUserId: String,
    mainTaskId: String,
    mainDefId: String,
    formData: Object,
    vars: Object,
    appId:String,
  },
  created(){
    this.initParams();
  },
  methods:{
      i18n(name,text,key){
          return this.$ti18(name,text,"bpmTaskLang.bpmOther",key);
      },
    initParams() {
      //从属性传入，这个一般用于 Util.open的方式打开。
      if (this.defId) {
        this.localDefId = this.defId;
        this.initProcessConfig();
      }
      //从弹框打开,使用DialogView 打开
      else if (this.menuParams) {
        this.handParams(JSON.parse(this.menuParams));
      } else if (this.config) {
        if (this.config.defId) {
          this.localDefId = this.config.defId;
        }
        if (this.config.defKey) {
          this.defKey = this.config.defKey;
        }
        this.initProcessConfig();
      }
      //通过路由获取参数
      else {
        var params;
        if (this.$route.meta.params) {
          params = JSON.parse(this.$route.meta.params);
        } else {
          params = this.$route.params;
        }
        this.fromRoute = true;
        this.handParams(params);
      }

    },
    handParams(params) {
      if (params.defId) {
        this.localDefId = params.defId;
      }
      if (params.defKey) {
        this.defKey = params.defKey;
      }
      this.initProcessConfig();
    },
    async initProcessConfig() {
      let self_ = this;
      await BpmInstApi.getProcessConfig(this.localDefId, this.defKey).then(res => {
        this.Loading = false
        if (!res.success) {
          this.$message.warning(res.message);
          return;
        }
        self_.localDefId = res.data.defId;

        //设置启动标题.
        uni.setNavigationBarTitle({
              title: res.data.name
        });

        if (res.data) {
          var config = Util.deepClone(self_.processConfig);
          let buttonConfigs = [];
          for (let i = 0; i < res.data.buttonConfigs.length; i++) {
            //启动去除特送按钮
            if (res.data.buttonConfigs[i].type != "specialDelivery") {
              buttonConfigs.push(res.data.buttonConfigs[i]);
            }
          }
          res.data.buttonConfigs = buttonConfigs;
          self_.processConfig = Object.assign(config, res.data);
        }
        //加载表单。
        if (self_.processConfig.formType == "online") {
          self_.initForm();
        }
      })
    },
    async initForm() {
      let params = {defId: this.localDefId};
        // #ifdef H5
        let instId = this.instId ? this.instId : this.$route.params.instId;
        if (instId) {
          params.instId = this.instId;
        }
        // #endif
        // #ifdef APP-PLUS
        params.instId = this.instId;
        // #endif
      //如果从外部传入主键
      if (this.pkId) {
        params.pkId = this.pkId;
      }
      let res = await BpmInstApi.getViewByDefId(params);
      if (!res.success) {
        this.$message.warning(res.message);
        return;
      }
      let data = res.data;
      let formData = data.formData;
      let curUser = uni.getStorageSync("user");
       this.contextData = {
        type: "start",
        curUserId: curUser.userId,
        curUserName: curUser.fullName,
        account: curUser.account,
        deptId: curUser.deptId,
        curNodeId: "start",
        curNodeName:this.i18n('startNode','开始节点'),
        instId: this.instId,
        defId: this.localDefId,
        tenantId: curUser.tenantId,
        tenantLabel: curUser.tenantLabel,
         //当前是否为发起人
         starter:true
      };
      formData[0].data = Object.assign(formData[0].data, this.formData);
      this.resForm = formData[0];
      if(this.resForm.globalConfig){
        uni.setStorage({key: "globalConfig", data: JSON.parse(this.resForm.globalConfig)})
      }else {
        uni.setStorage({key: "globalConfig", data: {}})
      }
        let scriptJs = Util.replaceDefault(this.resForm.script);
        this.formJs = new Function('return ' + scriptJs)();
    },
  }

}
</script>

<style scoped>
.BpmInstStart{
  height: 100%;
}
</style>
